import logger from '../../../../common/logger';
import { ParameterDto } from '../dtos';
import { ParameterService } from '../service/parameterService';
import { SimulationParameter } from '../../simulation/type';
import { ParameterValidationResult, ParameterValidator } from './parameterValidator';
export class DatabaseParameterValidator implements ParameterValidator {

  async validate(product: string, parameters: SimulationParameter[]): Promise<ParameterValidationResult[]> {
    const validationResults: ParameterValidationResult[] = [];

    const parametersInfo = await new ParameterService().findByProduct(product);
    for (const parameter of parameters) {
      const parameterInfo = this.findParameterInfo(parameter, parametersInfo);
      if (parameterInfo) {
        validationResults.push({ product, parameter: parameter.name, valid: true });
      } else {
        const message = `product ${product} param(name=${parameter.name} is unknown to map which prog name)`;
        logger.error(message);
        validationResults.push({ product, parameter: parameter.name, valid: false, message });
      }
    }
    return validationResults;
  }

  findParameterInfo(parameter: SimulationParameter, parametersInfo: ParameterDto[]): ParameterDto {
    const name = parameter.name;
    for (const parameterInfo of parametersInfo) {
      if (parameterInfo.name === name) { return parameterInfo; }
    }
  }
}